﻿using Google.Protobuf;

namespace DotNetClient
{
    public enum MessageType : byte
    {
        MSG_REQUEST = 0,
        MSG_NOTIFY = 1,
        MSG_RESPONSE = 2,
        MSG_PUSH = 3
    }

    /// <summary>
    /// 自定义消息
    /// </summary>
    public class Message
    {
        public MessageType type;
        public string route;
        public byte id;
        public byte[] data;

        public Message(MessageType type, string route, byte id, byte[] data)
        {
            this.type = type;
            this.route = route;
            this.id = id;
            this.data = data;
        }

        public T decode<T>() where T : class, IMessage, new()
        {
            if (data != null && data.Length > 0)
            {
                T obj = new T();
                IMessage message = obj.Descriptor.Parser.ParseFrom(data);
                return message as T;
            }
            return null;
        }
    }

    /// <summary>
    /// 握手消息
    /// </summary>
    public class Message_Handshake
    {
        public float heartbeat = 0; // 心跳时间
        public string[] route = null; // 路由表
    }
}
